home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillip2 / display.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-04  |  17.9 KB  |  624 lines

  1.  
  2.  
  3.    /**************************************************
  4.    *
  5.    *   file d:\cips\display.c
  6.    *
  7.    *   Functions: This file contains
  8.    *      display_image
  9.    *      display_image_portion
  10.    *      display_menu_for_display_image
  11.    *      map_16_shades_of_gray
  12.    *      my_map_64_shades_of_gray
  13.    *      transform_the_colors
  14.    *
  15.    *   Purpose:
  16.    *      These functions display images on a the
  17.    *      monitor.
  18.    *
  19.    *      NOTE: This file is full of Microsoft
  20.    *            specific code.  The PC C compiler
  21.    *            makers all have their own routines
  22.    *            for making dots appear on the
  23.    *            screen.  I put the statement
  24.    *            MSC 6.0 next to these calls.
  25.    *
  26.    *
  27.    *   External Calls:
  28.    *      rtiff.c - read_tiff_image
  29.    *      cips.c - my_clear_text_screen
  30.    *      hist.c - zero_histogram
  31.    *               calculate_histogram
  32.    *               perform_histogram_equalization
  33.    *               display_histogram
  34.    *
  35.    *   Modifications:
  36.    *      17 June 1987 - created
  37.    *      August 1990 - extension modifications for use
  38.    *          in the C Image Processing System.
  39.    *
  40.    ***************************************************/
  41.  
  42.  
  43. #include "cips.h"
  44.  
  45.  
  46.  
  47.  
  48.  
  49.    /***************************
  50.    *
  51.    *   display_image(...
  52.    *
  53.    ****************************/
  54.  
  55.  
  56. display_image(file_name, image, il, ie, ll, le,
  57.               image_header, monitor_type, 
  58.               color_transform, invert, image_colors, 
  59.               display_colors, show_hist, title)
  60.    char    color_transform[],
  61.            file_name[],
  62.            monitor_type[],
  63.            title[];
  64.    int     display_colors,
  65.            image_colors,
  66.            invert,
  67.            il,
  68.            ie,
  69.            ll,
  70.            le,
  71.            show_hist;
  72.    short   image[ROWS][COLS];
  73.    struct  tiff_header_struct *image_header;
  74. {
  75.    char  channels[80],
  76.          response[80];
  77.  
  78.    int   a,
  79.          b,
  80.          c,
  81.          channel,
  82.          count,
  83.          display_mode,
  84.          dx_offset,
  85.          dy_offset,
  86.          key,
  87.          horizontal,
  88.          len,
  89.          max_horizontal,
  90.          max_vertical,
  91.          not_finished,
  92.          q,
  93.          r,
  94.          vertical,
  95.          x_offset,
  96.          y_offset;
  97.  
  98.    unsigned int block,
  99.                 color,
  100.                 i,
  101.                 j,
  102.                 x,
  103.                 y;
  104.    unsigned long histogram[256], new_hist[256];
  105.  
  106.  
  107.      /**********************************************
  108.      *
  109.      *   If you want to display the histogram and 
  110.      *   do not want to perform hist equalization, 
  111.      *   then zero the histogram for calculations.
  112.      *
  113.      **********************************************/
  114.  
  115.    if(  (show_hist == 1)   &&
  116.         (color_transform[0] != 'H'))
  117.       zero_histogram(histogram);
  118.  
  119.    not_finished = 1;
  120.    while(not_finished){
  121.  
  122.  
  123.       if(display_colors == 16){
  124.          if(monitor_type[0] == 'V'){
  125.             horizontal   = 4;
  126.         vertical = 6;
  127.         display_mode = VRES16COLOR; /* MSC 6.0 */
  128.          }  /* ends if V */
  129.          if(monitor_type[0] == 'E'){
  130.             horizontal   = 3;
  131.             vertical     = 6;
  132.             display_mode = ERESCOLOR; /* MSC 6.0 */
  133.          }  /* ends if E */
  134.  
  135.       }  /* ends if colors == 16 */
  136.  
  137.       else{
  138.          horizontal   = 2;
  139.          vertical     = 2;
  140.          display_mode = MAXCOLORMODE; /* MSC 6.0 */
  141.       }
  142.  
  143.         /********************************************
  144.         *
  145.         *   Somehow, my dx and dy offsets are 
  146.         *   backwards from my horizontal and vertical 
  147.         *   variables. Trying to center the images 
  148.         *   on the screen. March 21 1992
  149.         *
  150.         ********************************************/
  151.  
  152.       max_horizontal = (image_header->image_length+50)
  153.                        /COLS;
  154.       max_vertical   = (image_header->image_width+50)
  155.                        /ROWS;
  156.  
  157.       dy_offset = ((horizontal-max_horizontal)/2)
  158.                   *COLS + 50;
  159.       dx_offset = ((vertical-max_vertical)/2)
  160.                   *ROWS + 20;
  161.  
  162.       if(max_horizontal > horizontal) dy_offset = 0;
  163.       if(max_vertical > vertical)     dx_offset = 0;
  164.  
  165.       if(horizontal > max_horizontal) 
  166.          horizontal = max_horizontal;
  167.       if(vertical > max_vertical)     
  168.          vertical   = max_vertical;
  169.  
  170.  
  171.         /****************************************
  172.         *
  173.         *   If color transform wants histogram
  174.         *   equalization, then read in the
  175.         *   image arrays and calculate the
  176.         *   histogram.   Zero both the histogram
  177.         *   and the new_hist.  You will need the
  178.         *   new_hist if you want to display the
  179.         *   equalized hist.
  180.         *
  181.         *****************************************/
  182.  
  183.       if(color_transform[0] == 'H'){
  184.          count = 1;
  185.          zero_histogram(histogram);
  186.          zero_histogram(new_hist);
  187.          for(a=0; a<vertical; a++){
  188.             for(b=0; b<horizontal; b++){
  189.  
  190.                x = a*COLS;
  191.                y = b*ROWS;
  192.  
  193.                printf(
  194.                   "\nDISPLAY> Calculating histogram");
  195.                printf(" %d of %d",
  196.                   count,vertical*horizontal);
  197.                count++;
  198.                read_tiff_image(file_name, image, il+y,
  199.                             ie+x, ll+y, le+x);
  200.                calculate_histogram(image, histogram);
  201.  
  202.             }  /* ends loop over b */
  203.          }  /* ends loop over a */
  204.       }  /* ends if display_mode == H */
  205.  
  206.  
  207.         /* set graphics mode */
  208.  
  209.    my_setvideomode(display_mode); /* MSC 6.0 */
  210.    if(display_colors == 16) 
  211.       map_16_shades_of_gray(display_mode);
  212.    if(display_colors == 256) 
  213.       my_map_64_shades_of_gray();
  214.  
  215.  
  216.         /****************************************
  217.         *
  218.         *   Loop over this size and
  219.         *   read and display ROWSxCOLS arrays.
  220.         *
  221.         *   If you want to show the histogram AND
  222.         *   do not want to do hist equalization
  223.         *   then calculate the hist from the
  224.         *   original image array.
  225.         *
  226.         *   If you want to do hist equalization
  227.         *   then calculate the new_hist AFTER
  228.         *   the image has been equalized by the
  229.         *   the transform_the_colors function.
  230.         *
  231.         *   NOTE: Remember that the function
  232.         *   transform_the_colors changes the
  233.         *   gray shade values in image array.
  234.         *
  235.         *****************************************/
  236.  
  237.         /*****************************************
  238.         *
  239.         *   These statements place a gray 
  240.         *   background across the display area of 
  241.         *   a VGA screen.  This reduces the 
  242.         *   contrast between the screen background 
  243.         *   and the images you display. This makes 
  244.         *   it easier to take photos.
  245.         *
  246.         *******************************************/
  247.  
  248.       /* MSC 6.0 */
  249.       my_setlinestyle(0xFFFF);
  250.       my_setcolor(10);
  251.       for(i=0; i<640;i++){
  252.          my_moveto(i, 0);
  253.          my_lineto(i, 480);
  254.       }
  255.  
  256.       for(a=0; a<vertical; a++){
  257.          for(b=0; b<horizontal; b++){
  258.  
  259.             x = a*COLS;
  260.             y = b*ROWS;
  261.             read_tiff_image(file_name, image, il+y,
  262.                             ie+x, ll+y, le+x);
  263.             if(  (show_hist == 1)  &&
  264.                  (color_transform[0] != 'H'))
  265.                calculate_histogram(image, histogram);
  266.  
  267.             transform_the_colors(image, 
  268.                                  color_transform,
  269.                                  display_colors,
  270.                                  image_colors, 
  271.                                  histogram,
  272.                                  horizontal, 
  273.                                  vertical);
  274.  
  275.             if(color_transform[0] == 'H')
  276.                calculate_histogram(image, new_hist);
  277.           display_image_portion(image, x+dx_offset,
  278.                                 y+dy_offset, 
  279.                                 display_colors,
  280.                                 image_colors, invert);
  281.          }        /* ends loop over b */
  282.       }        /* ends loop over a */
  283.  
  284.           /*******************************************
  285.           *
  286.           *   Put in these statements to print a title 
  287.           *   at the bottom of the display.  This is 
  288.           *   nice for taking photos or articles 
  289.           *   because it tells the reader what you 
  290.           *   are trying to do.
  291.           *
  292.           ********************************************/
  293.  
  294.       /* MSC 6.0 */
  295.       my_settextcolor(10);
  296.       my_setbkcolor(1L);
  297.       len = strlen(title);
  298.       len = 40 - (len/2);
  299.       my_settextposition(28, len);
  300.       my_outtext(title);
  301.  
  302.          /***************************
  303.          *
  304.          *   if show_hist == 1 then
  305.          *   display the histogram
  306.          *   in the lower right hand
  307.          *   corner of screen
  308.          *
  309.          *   If hist equalization was
  310.          *   performed then show the
  311.          *   new_hist.  If it wasn't
  312.          *   done then show the original
  313.          *   histogram.
  314.          *
  315.          ****************************/
  316.  
  317.       if(show_hist == 1){
  318.          if(monitor_type[0] == 'V')
  319.             y_offset = 470;
  320.          if(monitor_type[0] == 'E')
  321.             y_offset = 310;
  322.          x_offset = 380;
  323.          if(color_transform[0] == 'H')
  324.             display_histogram(new_hist, x_offset,
  325.                    y_offset, 5, 9);
  326.          else{
  327.             display_histogram(histogram, x_offset,
  328.                    y_offset, 5, 9);
  329.                /* xtra stuff for cips9
  330.             display_histogram(histogram, 45,
  331.                    370, 5, 9); */
  332.  
  333.                /*********************************
  334.                *
  335.                *   Change to show smoothed
  336.                *   histogram next to the regular
  337.                *   histogram.
  338.                *
  339.                **********************************/
  340.  
  341.                /* xtra stuff for cips9
  342.             smooth_histogram(histogram);
  343.             display_histogram(histogram, 345,
  344.                    370, 5, 9); */
  345.          }
  346.       }
  347.  
  348.       gets(response);
  349.       printf("\nEnter 0 to quit 1 to do again");
  350.       get_integer(¬_finished);
  351.  
  352.           /* set display back to text mode */
  353.       my_clear_text_screen();
  354.  
  355.  
  356.    }  /* ends while not_finished  */
  357.  
  358. }  /* ends main  */
  359.  
  360.  
  361.  
  362.  
  363.    /***********************************************
  364.    *
  365.    *   display_menu_for_display_image(
  366.    *
  367.    ************************************************/
  368.  
  369. display_menu_for_display_image(image_colors, 
  370.                               display_colors,
  371.                               invert, color_transform,
  372.                               monitor_type,
  373.                               show_hist)
  374.    char color_transform[], monitor_type[];
  375.    int  *invert, *image_colors, 
  376.         *display_colors, *show_hist;
  377. {
  378.    char response[80];
  379.    int  int_response, not_finished, r;
  380.  
  381.    not_finished = 1;
  382.    while(not_finished){
  383.       printf("\n\nDISPLAY> Enter choice "
  384.              "(0 for no change) ");
  385.       printf("\nDISPLAY> 1. Invert is %d (1=on 0=off)",
  386.              *invert);
  387.       printf("\nDISPLAY> 2. Color Transform-- %s",
  388.              color_transform);
  389.       printf("\nDISPLAY> 3. Input image has %d colors",
  390.              *image_colors);
  391.       printf("\nDISPLAY> 4. Display will show %d colors",
  392.              *display_colors);
  393.       printf("\nDISPLAY> 5. Monitor type is %s",
  394.              monitor_type);
  395.       printf("\nDISPLAY> 6. Histogram is %d", 
  396.              *show_hist);
  397.       printf("  (1=show 0=don't show)");
  398.       printf("\nDISPLAY> _\b");
  399.       get_integer(&r);
  400.  
  401.       if(r == 0){
  402.          not_finished = 0;
  403.       }
  404.  
  405.       if(r == 1){
  406.          printf("\nDISPLAY> Enter 1 for invert on");
  407.          printf(" 0 for invert off");
  408.          printf("\nDISPLAY> ___");
  409.          get_integer(&int_response);
  410.          *invert = int_response;
  411.       }  /* ends if r == 1 */
  412.  
  413.       if(r == 2){
  414.          printf("\nDISPLAY> Enter the new color "
  415.                 "transform mode ");
  416.          printf("\nDISPLAY> (S) Straight mode");
  417.          printf("   (H) Histogram Equalization");
  418.          printf("\nDISPLAY> _\b");
  419.          gets(response);
  420.          if((response[0] == 'S') ||
  421.             (response[0] == 's'))
  422.                strcpy(color_transform, 
  423.                       "Straight mode");
  424.          else
  425.                strcpy(color_transform,
  426.                 "Histogram Equalization mode");
  427.       }  /* ends if r == 2  */
  428.  
  429.       if(r == 3){
  430.          printf("\nDISPLAY> Enter the number "
  431.                 "of colors");
  432.          printf(" in the input image");
  433.          printf("\nDISPLAY> ___");
  434.          get_integer(&int_response);
  435.          *image_colors = int_response;
  436.       }  /* ends if r == 3 */
  437.  
  438.       if(r == 4){
  439.          printf(
  440.           "\nDISPLAY> Enter the number of colors "
  441.           "for the display");
  442.          printf("\nDISPLAY> ___");
  443.          get_integer(&int_response);
  444.          *display_colors = int_response;
  445.       }  /* ends if r == 4 */
  446.  
  447.       if(r == 5){
  448.          printf("\nDISPLAY> Enter the new monitor type");
  449.          printf("\nDISPLAY> (E) EGA   (V) VGA");
  450.          printf("   (C) CGA   (M) Monochrome");
  451.          printf("\nDISPLAY> _\b");
  452.          gets(response);
  453.          if((response[0] == 'E') ||
  454.             (response[0] == 'e'))
  455.             strcpy(monitor_type, "EGA");
  456.       if((response[0] == 'V') ||
  457.          (response[0] == 'v'))
  458.             strcpy(monitor_type, "VGA");
  459.       if((response[0] == 'C') ||
  460.          (response[0] == 'c'))
  461.             strcpy(monitor_type, "CGA");
  462.       if((response[0] == 'M') ||
  463.          (response[0] == 'm'))
  464.             strcpy(monitor_type, "Monochrome");
  465.       }  /* ends if r == 5  */
  466.  
  467.       if(r == 6){
  468.          printf(
  469.             "\nDISPLAY> Enter 1 for show histogram "
  470.             "0 for don't");
  471.          printf("\nDISPLAY> ___");
  472.          get_integer(&int_response);
  473.          *show_hist = int_response;
  474.       }  /* ends if r == 6 */
  475.  
  476.    }  /* ends while not_finished  */
  477. }  /* ends display_menu  */
  478.  
  479.  
  480.  
  481.  
  482.    /********************************
  483.    *
  484.    *   display_image_portion(...
  485.    *
  486.    *********************************/
  487.  
  488. display_image_portion(image, x, y, display_colors, 
  489.                       image_colors, invert)
  490.    int      invert, display_colors, image_colors;
  491.    short    image[ROWS][COLS];
  492.    unsigned int x, y;
  493. {
  494.    unsigned int color, i, j;
  495.  
  496.       if(invert == 1){
  497.         for(i=0; i<ROWS; i++)
  498.            for(j=0; j<COLS; j++)
  499.               image[i][j] = (display_colors-1)
  500.                              - image[i][j];
  501.       }  /* ends if invert == 1 */
  502.  
  503.       for(i=0; i<ROWS; i++){
  504.          for(j=0; j<COLS; j++){
  505.  
  506.         /* MSC 6.0 */
  507.         my_setcolor(image[i][j]);
  508.         my_setpixel(j+x, i+y);
  509. /*****
  510. my_set_pixel(j+x, i+y, image[i][j]);
  511. ******/
  512.  
  513.          }  /* ends loop over j  */
  514.       }     /* ends loop over i  */
  515.  
  516. }  /* ends display_image_portion  */
  517.  
  518.  
  519.  
  520.  
  521.  
  522.    /**********************************************
  523.    *
  524.    *   map_16_shades_of_gray(...
  525.    *
  526.    *   This function maps 16 shades of gray into
  527.    *   the first 16 color indices.  This allows
  528.    *   you to display a true "black and white"
  529.    *   image on a color monitor.
  530.    *
  531.    *********************************************/
  532.  
  533. map_16_shades_of_gray(display_mode)
  534.    int display_mode;
  535. {
  536.    /* all MSC 6.0 statements */
  537. my_setvideomode(display_mode);
  538. my_remappalette(0,  0x000000L);
  539. my_remappalette(1,  0x040404L);
  540. my_remappalette(2,  0x080808L);
  541. my_remappalette(3,  0x0c0c0cL);
  542. my_remappalette(4,  0x101010L);
  543. my_remappalette(5,  0x141414L);
  544. my_remappalette(6,  0x181818L);
  545. my_remappalette(7,  0x1c1c1cL);
  546. my_remappalette(8,  0x202020L);
  547. my_remappalette(9,  0x242424L);
  548. my_remappalette(10, 0x282828L);
  549. my_remappalette(11, 0x2c2c2cL);
  550. my_remappalette(12, 0x303030L);
  551. my_remappalette(13, 0x343434L);
  552. my_remappalette(14, 0x383838L);
  553. my_remappalette(15, 0x3f3f3fL);
  554. }
  555.  
  556.  
  557.  
  558.  
  559.    /*********************************************
  560.    *
  561.    *   transform_the_colors(...
  562.    *
  563.    *   This function transforms the gray shades
  564.    *   in the image array for display.  It can either
  565.    *   do it in straight mode by multiplying or
  566.    *   dividing or it can do it with hist
  567.    *   equalization by calling the function
  568.    *   perform_histogram_equalization.
  569.    *
  570.    *************************************************/
  571.  
  572. transform_the_colors(image, color_transform,
  573.                      display_colors, image_colors,
  574.                      histogram, horizontal,
  575.                      vertical)
  576.    char   color_transform[];
  577.    int    display_colors, horizontal,
  578.           image_colors, vertical;
  579.    short  image[ROWS][COLS];
  580.    unsigned long histogram[];
  581. {
  582.    int         color, i, j;
  583.    float new_grays, area;
  584.    unsigned long x;
  585.  
  586.    if(color_transform[0] == 'S'){
  587.       for(i=0; i<ROWS; i++){
  588.          for(j=0; j<COLS; j++){
  589.  
  590.             if( (display_colors == 16) &&
  591.                 (image_colors  == 256))
  592.                color = image[i][j]/16;
  593.             if( (display_colors == 16) &&
  594.                 (image_colors  == 16))
  595.                color = image[i][j];
  596.             if( (display_colors == 256) &&
  597.                 (image_colors  == 256))
  598.                color = image[i][j];
  599.             if( (display_colors == 256) &&
  600.                 (image_colors  == 16))
  601.                color = image[i][j]*16;
  602.  
  603.             image[i][j] = color;
  604.  
  605.          }  /* ends loop over j        */
  606.       }        /* ends loop over i        */
  607.    }  /* ends if transform is straight */
  608.  
  609.    if(color_transform[0] == 'H'){
  610.  
  611.       area      = ((long)(vertical)) *
  612.                   ((long)(horizontal));
  613.       area      = area*10000.0;
  614.       new_grays = display_colors;
  615.  
  616.       perform_histogram_equalization(image, histogram,
  617.                                      new_grays, area);
  618.  
  619.    }  /* ends if transform is hist equalization */
  620.  
  621. }  /* ends transform_the_colors */
  622.  
  623.  
  624.